var express = require('express');
var router = express.Router();
var path = require('path');
var app = express();
const db = require('../database/db.js');
var mongo = require('mongodb');
var notice = require('./api/noticeApi.js')
const user = require('./api/userApi.js')
const question = require('./api/questionApi.js')
const examination = require('./api/examinationApi.js')
const paper = require('../bin/models/paperModels');



/* 提交的信息 */
router.post('/tijiao_article', function (req, res, next) { // 插入文章
  // mongodb.find('shiziduiwu',{name:'师资队伍'});
  db.insert('articles', req.body);
  res.send('提交成功');
});

router.post('/login', function (req, res, next) {
  res.send({ message: '登录成功', username: req.body.username, isLogin: true });
  // db.find('user', req.body, (resu) => {
  //   console.log(resu.length);
  //   if (resu.length != 0) {
  //     res.send({ message: '登录成功', username: resu.username, isLogin: true });
  //   }
  //   else {
  //     res.send({ message: '用户名或密码错误', username: resu.username, isLogin: false });
  //   }
  // });
});

router.post('/drop_article', function (req, res, next) {
  var _id = mongo.ObjectID(req.body._id)
  console.log(_id);
  // mongodb.find('shiziduiwu',{name:'师资队伍'});
  db.drop('articles', { _id: _id }, () => {
    res.send('删除成功');
  })
});

router.post('/addPaper', function (req, res, next) {
  console.log(req.body)
  paper.insert(req.body, (res2) => {
   res.send(res2)
 })
});

router.post('/updata_article', function (req, res, next) {
  var _id = mongo.ObjectID(req.body._id);
  console.log(_id);
  db.update('articles',{_id:_id},{$set:{article:req.body.article}},()=>{
    res.send('修改成功');
  })
});

router.use(notice)
router.use(user)
router.use(question)
router.use(examination)
module.exports = router;
